package com.huiminpay.paymentagent.service.impl;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.paymentagent.api.conf.AliConfigParam;
import com.huiminpay.paymentagent.api.dto.AlipayBean;
import com.huiminpay.paymentagent.api.dto.AlipayTradeQueryDto;
import com.huiminpay.paymentagent.api.service.IAlipayAgentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;

@Service
@Slf4j
public class AlipayAgentServiceImpl implements IAlipayAgentService {


    @Override
    public String doAlipayWepApi(AliConfigParam aliConfigParam, AlipayBean alipayBean) throws BusinessException {

        /**
         * 阿里网关
         */
        String serverUrl = aliConfigParam.getUrl();
        /**
         * 应用ID
         */
        String appId = aliConfigParam.getAppId();
        /**
         * 私钥
         */
        String privateKey = aliConfigParam.getRsaPrivateKey();
        /**
         * 格式JSON
         */
        String format = aliConfigParam.getFormat();
        /**
         * 字符编码
         */
        String charset = aliConfigParam.getCharest();
        /**
         * 支付宝公钥
         */
        String aliPayPublicKey = aliConfigParam.getAlipayPublicKey();
        /**
         * RSA2 户生成签名字符串所使用的签名算法类型
         */
        String signType = aliConfigParam.getSigntype();

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format,
                charset, aliPayPublicKey, signType);

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

        if(StringUtils.isNotEmpty(aliConfigParam.getNotifyUrl())) {
            //设置异步通知地址
            request.setNotifyUrl(aliConfigParam.getNotifyUrl());
        }
        //request.setBizContent();
        //设置请求参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setSubject(alipayBean.getSubject());
        /**
         * 商户订单号，必填
         */
        model.setOutTradeNo(alipayBean.getOutTradeNo());
        /**
         * 付款金额，必填
         */
        model.setTotalAmount(alipayBean.getTotalAmount());
        //用户支付中途退出返回商户地址
        model.setQuitUrl("---");
        model.setProductCode(alipayBean.getProductCode());

        request.setBizModel(model);


        try {
            AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
            if (response.isSuccess()){
                //执行成功
                return response.getBody();
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            throw new BusinessException(CommonErrorCode.UNKOWN);
        }

        throw new BusinessException(CommonErrorCode.UNKOWN);
    }

    @Override
    public AlipayTradeQueryDto queryAlipayOrderByOutTradeNo(AliConfigParam aliConfigParam,
                                                            String outTradeNo) {

        /**
         * 阿里网关
         */
        String serverUrl = aliConfigParam.getUrl();
        /**
         * 应用ID
         */
        String appId = aliConfigParam.getAppId();
        /**
         * 私钥
         */
        String privateKey = aliConfigParam.getRsaPrivateKey();
        /**
         * 格式JSON
         */
        String format = aliConfigParam.getFormat();
        /**
         * 字符编码
         */
        String charset = aliConfigParam.getCharest();
        /**
         * 支付宝公钥
         */
        String aliPayPublicKey = aliConfigParam.getAlipayPublicKey();
        /**
         * RSA2 户生成签名字符串所使用的签名算法类型
         */
        String signType = aliConfigParam.getSigntype();


        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,aliPayPublicKey,signType);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        //request.setBizContent();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(outTradeNo);
        request.setBizModel(model);

        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if (response.isSuccess()) {
                System.out.println("调用成功");
                AlipayTradeQueryDto alipayTradeQueryDto = new AlipayTradeQueryDto();
                alipayTradeQueryDto.setOutTradeNo(outTradeNo);
                alipayTradeQueryDto.setTradeNo(response.getTradeNo());
                //获取支付宝支付状态
                alipayTradeQueryDto.setTradeStatus(response.getTradeStatus());
                return alipayTradeQueryDto;
            } else {
                System.out.println("调用失败");
                log.info("{}", JSON.toJSONString(response));
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        throw new BusinessException(CommonErrorCode.UNKOWN);
    }


}
